<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width: 300px" />
      </el-form-item>

      <el-form-item label="密码:" prop="pass">
        <el-input
          v-model="userInfo.pass"
          type="password"
          style="width: 300px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updateUser">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api'
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        pass: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        pass: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.getUserDetailFn()
  },
  methods: {
    // 获取员工基本信息
    async getUserDetailFn() {
      const res = await getUserDetailByIdAPI(this.$route.query.id)
      this.userInfo = { ...res.data, pass: '' }
    },
    // 更新员工的事件
    updateUser() {
      this.$refs.userForm.validate(async(valid) => {
        if (!valid) return this.$message.error('检查用户名和密码')
        if (this.userInfo.pass.trim().length > 0) this.userInfo.password = this.userInfo.pass
        console.log(this.userInfo)
        const res = await saveUserDetailByIdAPI({
          ...this.userInfo
        })
        if (res.success) this.$message.success(res.message)
        this.userInfo.pass = ''
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
